﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql;
using MySql.Data;
using MySql.Data.MySqlClient;
using MySql.Data.Entity;
using ASV.Classes;
using System.Data;

namespace ASV
{
    public partial class Bearbeiten : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            Authentication auth = new Authentication();
            int userGroup;
            int allowedGroup;
            Connection mycon = new Connection();
            DataTable datatable = new DataTable();
            object[] values = null;

            string selectedID = Request.QueryString["ID"];


            if (auth.isLoggedIn() == true)
            {
                userGroup = int.Parse(HttpContext.Current.Session["benutzertyp"].ToString());
                allowedGroup = 2;
                if (auth.checkUserGroup(userGroup, allowedGroup) == false)
                {
                    Response.Redirect("~/Error.aspx");
                }
            }
            else
            {
                Response.Redirect("~/Error.aspx");
            }

            datatable = mycon.select("SELECT `name`,`beschreibung`,`Seriennummer`,`preis`,`bildpfad` FROM `produkte` WHERE ID='" + selectedID + "'");

            values = datatable.Rows[0].ItemArray;

            NameID.Text = values[0].ToString();

            BeschreibungID.InnerText = values[1].ToString();
            SeriennummerID.Text = values[2].ToString();
            PreisID.Text = values[3].ToString();
        }

        protected void EditButton_Click(object sender, EventArgs e)
        {
            Connection mycon = new Connection();

            DataTable datatable = new DataTable();
            string name = NameID.Text;
            string beschreibung = BeschreibungID.InnerText;
            string seriennummer = SeriennummerID.Text;
            string preis = PreisID.Text;
            string selectedID = Request.QueryString["ID"];
            string fn;
            string SaveLocation;

            if ((UrlID2.PostedFile != null) && (UrlID2.PostedFile.ContentLength > 0))
            {
                fn = System.IO.Path.GetFileName(UrlID2.PostedFile.FileName);
                SaveLocation = Server.MapPath("/img") + "\\" + fn;
                try
                {
                    UrlID2.PostedFile.SaveAs(SaveLocation);
                    Response.Write("The file has been uploaded.");
                }
                catch (Exception ex)
                {
                    Response.Write("Error: " + ex.Message);
                }

                mycon.insertUpdate("UPDATE `produkte` SET `Name`='" + name + "', `beschreibung`='" + beschreibung + "', `Seriennummer`='" + seriennummer + "',`preis`='" + preis + "',`bildpfad`='img/" + UrlID2.PostedFile.FileName + "' WHERE `id`= '" + selectedID + "'");
            }
            else
            {
                datatable = mycon.select("SELECT `bildpfad` FROM `produkte` WHERE ID='" + selectedID + "'");
                
                if (datatable.Rows.Count != 0)
                {
                    mycon.insertUpdate("UPDATE `produkte` SET `Name`='" + name + "', `beschreibung`='" + beschreibung + "', `Seriennummer`='" + seriennummer + "',`preis`='" + preis + "',`bildpfad`='" + datatable.Rows[0].ItemArray[0] + "' WHERE `id`= '" + selectedID + "'");
                }
                else
                {
                    mycon.insertUpdate("UPDATE `produkte` SET `Name`='" + name + "', `beschreibung`='" + beschreibung + "', `Seriennummer`='" + seriennummer + "',`preis`='" + preis + "',`bildpfad`='img/140x140.png' WHERE `id`= '" + selectedID + "'");
                }
            }
        Response.Redirect("~/Admin/Produkte.aspx");
        }
    }
}